//
//  ItemView.m
//  口袋
//
//  Created by why on 15/5/25.
//  Copyright (c) 2015年 口袋. All rights reserved.
//

#import "ItemView.h"
#import "NewsCell.h"
#import "NewsScrollView.h"
#import "SANetWorkingTask.h"
#import "NewsModel.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "NewsTopView.h"
#import "NewsPicCell.h"
#import "HeadCell.h"
#import "ShowImageViewController.h"
#import <MJRefresh/MJRefresh.h>

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface ItemView ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) NSMutableArray * array;

@property (nonatomic, retain) UITableView * table;

@end

@implementation ItemView

- (void)dealloc
{
    [_array release];
    [_table release];
    [_dic release];
    [super dealloc];
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.dic = [NSDictionary dictionary];
        self.array = [NSMutableArray array];
        
    }
    return self;
}
- (void)start
{
    [self createConnect];

}
- (void)createConnect
{
    [SANetWorkingTask request:[_dic objectForKey:@"url"] parmater:nil block:^(NSDictionary *result) {
        NSArray * array = [result objectForKey:[_dic objectForKey:@"name"]];
        for (NSDictionary * dic in array) {
            NewsModel * model = [[NewsModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [_array addObject:model];
            [model release];
        }
        [self createTable];
    }];
}

- (void)createTable
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
    _table.rowHeight = HEIGHT / 6.67;
    _table.dataSource = self;
    _table.delegate = self;
    [self addSubview:_table];
    [_table release];
    
    
    // 头条滚动scroll
    if ([[_dic objectForKey:@"title"] isEqualToString:@"头条"]) {
        NewsScrollView * myscroll = [[NewsScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT * 0.4)];
    _table.tableHeaderView = myscroll;
    [myscroll release];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _array.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsModel * model = [_array objectAtIndex:indexPath.row];
    
    if (model.hasHead == YES && ![model.tname isEqualToString:@"头条"]) {
        return HEIGHT * 0.4;
    } else if ([model.skipType isEqualToString:@"photoset"]) {
        return HEIGHT / 5.5;
    } else {
        return HEIGHT / 6.67;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsModel * model = [_array objectAtIndex:indexPath.row];
    
    static NSString * cellIdentify = @"cell";
    // 头条headerView
    if (model.hasHead == YES && ![model.tname isEqualToString:@"头条"]) {
        
        HeadCell * cell = [[HeadCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell2"];
        [cell.imageView1 sd_setImageWithURL:[NSURL URLWithString:model.imgsrc] placeholderImage:nil];
        cell.label.text = model.title;
        return cell;
        
        // 照片形式的cell
    } else if ([model.skipType isEqualToString:@"photoset"]) {
        
        NewsPicCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        if (!cell) {
            cell = [[NewsPicCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell1"];
        }
        NSDictionary * dic1 = [model.imgextra firstObject];
        NSDictionary * dic2 = [model.imgextra lastObject];
        NSString * path1 = [dic1 objectForKey:@"imgsrc"];
        NSString * path2 = [dic2 objectForKey:@"imgsrc"];
        
        [cell.imageView1 sd_setImageWithURL:[NSURL URLWithString:model.imgsrc] placeholderImage:nil];
        [cell.imageView2 sd_setImageWithURL:[NSURL URLWithString:path1] placeholderImage:nil];
        [cell.imageView3 sd_setImageWithURL:[NSURL URLWithString:path2] placeholderImage:nil];
        cell.label.text = model.title;
        
        // 跟帖数
        NSNumber * a = model.replyCount;
        NSInteger b = [a integerValue];
        if (b > 10000) {
            float c = b * 1.0 / 10000 * 1.0;
            cell.comments.text = [NSString stringWithFormat:@"%.1f 万跟帖", c];
        } else {
            cell.comments.text = [NSString stringWithFormat:@"%ld 跟帖", (long)b];
        }
        return cell;

    } else {
        // 普通形式的cell
        NewsCell * cell = [tableView dequeueReusableCellWithIdentifier:cellIdentify];
        if (!cell) {
            cell = [[NewsCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentify];
        }
        NSURL * url = [NSURL URLWithString:model.imgsrc];
        [cell.imgsrc sd_setImageWithURL:url placeholderImage:nil];
        cell.title.text = model.title;
        cell.digest.text = model.digest;
        
        // 跟帖数
        NSNumber * a = model.replyCount;
        NSInteger b = [a integerValue];
        if (b > 10000) {
            float c = b * 1.0 / 10000 * 1.0;
            cell.comments.text = [NSString stringWithFormat:@"%.1f 万跟帖", c];
        } else {
            cell.comments.text = [NSString stringWithFormat:@"%ld 跟帖", (long)b];
        }
        return cell;
    }
}

// cell点击事件  代理传值回controller
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewsModel * model = [_array objectAtIndex:indexPath.row];
        [self.delegate selectCell:indexPath.row model:model];
}

@end
